<?php

class Index extends Controller {

	public function run() {
		$pluginVers = $this->_getPluginVers();

		$config = Sdk\lib\Config::GetRoot();

		$var = array(
			'witUrl' => WitURL,
			'pluginVers' => $pluginVers,
			'pluginInfo' => Sdk\lib\Core::GetInfo($pluginVers),
			'witPid' => !empty($config['base']['witPid']) ? $config['base']['witPid'] : 0,
		);
		$this->template('index', $var);
	}

	private function _getPluginVers() {
		$pluginRoot = ROOT_PATH . '/www/';

		$plugins = array();
		$f = opendir($pluginRoot);
		while (($plugin = readdir($f)) !== false) {
			if ($plugin == '.' || $plugin == '..') {
				continue;
			}
			$pluginDir = $pluginRoot . '/' . $plugin;
			if (!is_dir($pluginDir)) {
				continue;
			}
			$fsub = opendir($pluginDir);
			$vers = array();
			while (($ver = readdir($fsub)) !== false) {
				if ($ver == '.' || $ver == '..') {
					continue;
				}
				$verDir = $pluginDir . '/' . $ver . '/controllers';
				if (!is_dir($verDir)) {
					continue;
				}
				$vers[] = $ver;
			}
			closedir($fsub);
			$plugins[$plugin] = $vers;
		}
		closedir($f);
		return $plugins;
	}
}